﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace _2D
{
    public class Level
    {
        private string _xmlFile;
        private XmlDocument levelXML;
        private Background[] backgroundsCollection;
        private ContentManager _content;
        private int[] currentY;     // Guardara la posición de la Y de cada background según el nivel en el que se encuentren.
        public Rectangle[] collisions;

        public Level(string xmlFile, ContentManager content)
        {
            _xmlFile = xmlFile;
            _content = content;
        }

        public void Load()
        {
            levelXML = new XmlDocument();
            levelXML.Load(_xmlFile);

            //Cargamos BackGrounds
            XmlNodeList backgrounds = levelXML.GetElementsByTagName("background");

            int i = 0;
            backgroundsCollection = new Background[backgrounds.Count];
            currentY = new int[backgrounds.Count];

            foreach (XmlElement nodo in backgrounds)
            {
                int currentLevel = int.Parse(nodo.GetElementsByTagName("level")[0].InnerText);
                int currentYvalue = currentY[currentLevel];
                bool final = Boolean.Parse(nodo.GetElementsByTagName("final")[0].InnerText);
                
                currentY[currentLevel] += int.Parse(nodo.GetElementsByTagName("y")[0].InnerText);
                Background auxBg = new Background(new Vector2(int.Parse(nodo.GetElementsByTagName("x")[0].InnerText),int.Parse(nodo.GetElementsByTagName("y")[0].InnerText)),
                    _content, nodo.GetElementsByTagName("name")[0].InnerText,
                    int.Parse(nodo.GetElementsByTagName("shift")[0].InnerText), currentYvalue, final);
                backgroundsCollection[i] = auxBg;
                i++;
            }

            XmlNodeList collisionsXML = levelXML.GetElementsByTagName("collision");

            i = 0;
            collisions = new Rectangle[collisionsXML.Count];

            foreach (XmlElement nodo in collisionsXML)
            {
                Rectangle auxRect = new Rectangle(int.Parse(nodo.GetElementsByTagName("x")[0].InnerText),
                    int.Parse(nodo.GetElementsByTagName("y")[0].InnerText),
                    int.Parse(nodo.GetElementsByTagName("w")[0].InnerText),
                    int.Parse(nodo.GetElementsByTagName("h")[0].InnerText));
                collisions[i] = auxRect;
                i++;
            }
        }

        public void Draw(SpriteBatch batch)
        {
            // Dibujamos los Backgrounds
            foreach(Background bg in backgroundsCollection)
            {
                bg.Draw(batch);
            }
        }

        public void Update(KeyboardState keyboard, bool moveLevelComponents)
        {
            // Actualizamos los Backgrounds
            foreach (Background bg in backgroundsCollection)
            {
                bg.Update(keyboard, moveLevelComponents);
            }
            // Actulizamos las colisiones
            if (moveLevelComponents)
            {
                for (int i = 0; i < collisions.Count(); i++)
                {
                    if (keyboard.IsKeyDown(Keys.Up))
                    {
                        collisions[i].Y += 2;
                    }
                }
            }
        }
        
    }
}
